package be.ipl.eBar.servlets;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import be.ipl.eBar.exceptions.EtatConversationnelIncompletException;
import be.ipl.eBar.usecases.GestionDUneCommande;

/**
 * Servlet implementation class SauverCommande
 */
@WebServlet(name = "sauve.html")
public class SauverCommande extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SauverCommande() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Context jndi = new InitialContext();
			GestionDUneCommande gduc = (GestionDUneCommande) jndi
					.lookup("eBar6_EAR/GestionDUneCommandeImpl!be.ipl.eBar.usecases.GestionDUneCommande?stateful");
			HttpSession session = request.getSession();
			gduc.sauverCommande();
			session.removeAttribute("commande");
			request.getRequestDispatcher("index.html").forward(request, response);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (EtatConversationnelIncompletException e) {
			e.printStackTrace();
		}
	}

}
